//
//  FQAddBankCardViewController.swift
//  SwiftProject
//
//  Created by 董駸 on 2023/4/5.
//

import UIKit

class FQAddBankCardViewController: BaseViewController, UITableViewDataSource, UITableViewDelegate {
    let tableView = UITableView(frame: .zero, style: .plain)
    let agreeBtn = QMUIButton()
    var dataArray : [CellModel] = []
    
    override func viewDidLoad() {
        super.viewDidLoad()
        navigationBarType = .light
    }
    
    override func initSubviews() {
        super.initSubviews()
        
        let array = [["title" : "持卡人"], ["title" : "卡号"], ["title" : "卡类型"]]
        dataArray = array.map{ CellModel.deserialize(from: $0)! }
        
        tableView.delegate = self
        tableView.dataSource = self
        tableView.rowHeight = 50
        tableView.register(TextFieldTableViewCell.self, forCellReuseIdentifier: "cell")
        tableView.isScrollEnabled = false
        view.addSubview(tableView)
        
        tableView.snp.makeConstraints { make in
            make.left.right.equalToSuperview()
            make.top.equalTo(NavigationContentTop + 16)
            make.height.equalTo(50 * 3)
        }
        
        agreeBtn.setImage(UIImage(named: "icon_agreement_off"), for: .normal)
        agreeBtn.setImage(UIImage(named: "icon_agreement_on"), for: .selected)
        view.addSubview(agreeBtn)
        
        agreeBtn.snp.makeConstraints { make in
            make.left.equalTo(30)
            make.top.equalTo(tableView.snp.bottom).offset(48)
            make.size.equalTo(15)
        }
        agreeBtn.rx.tap.subscribe ({ [unowned self] _ in
            self.agreeBtn.isSelected.toggle()
//            self.loginViewModel.buttonIsSelected.accept(self.agreeBtn.isSelected)
        }).disposed(by: rx.disposeBag)
        
        let agreementLb = QMUILabel()
        agreementLb.isUserInteractionEnabled = true
        agreementLb.font = .rSFont(12)
        agreementLb.textColor = .color153
        view.addSubview(agreementLb)
        
        agreementLb.snp.makeConstraints { (make) in
            make.centerY.equalTo(agreeBtn)
            make.left.equalTo(agreeBtn.snp.right).offset(4)
            make.right.equalTo(-30)
        }

        var str : ASAttributedString = """
        已阅读并同意\("《用户协议》", .action { [unowned self] in
                    let vc = BaseWebViewController(.local("agreement01", "html"))
                    self.navigationController?.qmui_pushViewController(vc, animated: true)
        })
        """
        str.add(attributes: [.foreground(.colorMain)], checkings: [.action])
        agreementLb.attributed.text = str
        
        let button = QMUIFillButton(fill: .colorMain, titleTextColor: .white)
        button.setTitle("下一步", for: .normal)
        button.titleLabel?.font = .mSFont(20)
        button.addTarget(self, action: #selector(buttonClick), for: .touchUpInside)
        view.addSubview(button)
        
        button.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.size.equalTo(CGSize(width: 315, height: 52))
            make.top.equalTo(agreementLb.snp.bottom).offset(8)
        }
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        dataArray.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as! TextFieldTableViewCell
        cell.model = dataArray[indexPath.row]
        return cell
    }
    
    func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
        cell.separatorInset = .zero
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)
        if dataArray[indexPath.row].title == .cardType {
            let vc = FQBankCardTypeViewController()
            navigationController?.qmui_pushViewController(vc, animated: true)
        }
    }
    
    @objc func buttonClick() {
        
    }
    
    override func setupNavigationItems() {
        super.setupNavigationItems()
        title = "添加银行卡"
    }

}
